package com.gtw.modules.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbCategoryService;
import com.gtw.modules.api.service.TbPropertyService;
import com.gtw.modules.entity.TbCategory;
import com.gtw.modules.entity.TbProperty;
import com.gtw.modules.sys.DTO.PropertyDto;
import com.gtw.modules.sys.service.SysCommodityService;
import com.gtw.modules.entity.TbCommodity;
import com.gtw.modules.sys.service.SysPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商品表
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-01 10:46:46
 */
@RestController
@RequestMapping("/sys/commodity")
public class SysCommodityController extends AbstractController{
    @Autowired
    private SysCommodityService commodityService;

    @Autowired
    private TbCategoryService categoryService;

    @Autowired
    private SysPropertyService sysPropertyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:commodity:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbCommodity> pageUtil = new Page<TbCommodity>(query.getPage(), query.getLimit());
        Page<TbCommodity> page = commodityService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    /**
     * 查询所有商品
     * @return
     */
    @GetMapping("/findAll")
    public R findAll(){
        List<TbCommodity> list = commodityService.findAll();
        return R.ok().put("list",list);
    }

    /**
     * 查询所有封装属性
     * @return
     */
    @RequestMapping("/findaLLProperty")
    public R findaLLProperty(){
        List<TbProperty> list = sysPropertyService.findaLLProperty();
        List<PropertyDto> listdto = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            PropertyDto dto = new PropertyDto();
            TbProperty tbProperty = list.get(i);
            dto.setId(tbProperty.getId());
            dto.setMessage(tbProperty.getPropertyname()+":"+tbProperty.getPropertyvalue());
            listdto.add(dto);
        }
        return R.ok().put("listdto",listdto);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:commodity:info")
    public R info(@PathVariable("id") Integer id) {
        TbCommodity commodity = commodityService.queryObject(id);
        commodity.setPrice(commodity.getPrice()/100);
        commodity.setPricediscounts(commodity.getPricediscounts()/100);
        commodity.setFreight(commodity.getFreight()/100);
        return R.ok().put("commodity", commodity);
    }

    /**
     * 保存
     */
    @SysLog("添加商品")
    @RequestMapping("/save")
    @RequiresPermissions("sys:commodity:save")
    public R save(@RequestBody TbCommodity commodity) {
        TbCategory tbCategory = categoryService.selectByPrimaryKey(commodity.getCategoryid());
        commodity.setCategoryname(tbCategory.getCategoryyname());
        commodity.setCreatedate(new Date());
        commodity.setUpauthor(getUser().getUsername());
        commodity.setIsdelete(false);
        commodity.setPrice(commodity.getPrice()*100);
        commodity.setPricediscounts(commodity.getPricediscounts()*100);
        commodity.setFreight(commodity.getFreight()*100);
        commodityService.save(commodity);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改商品")
    @RequestMapping("/update")
    @RequiresPermissions("sys:commodity:update")
    public R update(@RequestBody TbCommodity commodity) {
        TbCategory tbCategory = categoryService.selectByPrimaryKey(commodity.getCategoryid());
        commodity.setCategoryname(tbCategory.getCategoryyname());
        commodity.setPrice(commodity.getPrice()*100);
        commodity.setPricediscounts(commodity.getPricediscounts()*100);
        commodity.setFreight(commodity.getFreight()*100);
        commodityService.update(commodity);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除商品")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:commodity:delete")
    public R delete(@RequestBody Integer[] ids) {
        commodityService.deleteBatch(ids);
        return R.ok();
    }

}
